package com.bodu.depress.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.exception.custom.DeleteException;
import com.bodu.core.exception.custom.SaveException;
import com.bodu.core.exception.custom.SearchException;
import com.bodu.core.exception.custom.UpdateException;
import com.bodu.depress.entity.request.DepressCollectRequest;
import com.bodu.depress.entity.request.DepressCollectResultRequest;
import com.bodu.depress.entity.vo.DepressCollectResultVo;
import com.bodu.depress.entity.vo.DepressCollectVo;
import com.bodu.depress.mapper.DepressCollectDetailMapper;
import com.bodu.depress.mapper.DepressCollectMapper;
import com.bodu.depress.mapper.DepressCollectResultMapper;
import com.bodu.depress.service.DepressCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service("depressCollectService")
public class DepressCollectServiceImpl implements DepressCollectService {

    @Resource
    private DepressCollectMapper depressCollectMapper;

    @Resource
    private DepressCollectDetailMapper depressCollectDetailMapper;

    @Resource
    private DepressCollectResultMapper depressCollectResultMapper;

    @Override
    public DepressCollectVo getModel(Long id) throws SearchException {
        DepressCollectVo vo = depressCollectMapper.getModel(id);
        vo.setStatusFlagName(StatusFlagEnum.getStatusValues(vo.getStatusFlag()));
        List<Long> questionIds = depressCollectDetailMapper.getQuestionIdsByCollectId(vo.getId());
        if (ObjectUtil.isEmpty(questionIds)) {
            questionIds = new ArrayList<>();
        }
        vo.setSelectIds(questionIds);

        List<DepressCollectResultVo> dcr = findResult(id);
        vo.setCollectResults(dcr);

        return vo;
    }

    private List<DepressCollectResultVo> findResult(Long collectId) {
        ParamReq<DepressCollectResultRequest> paramReq = new ParamReq<>();
        paramReq.setSize(100);
        paramReq.setCurrent(1);
        DepressCollectResultRequest depressCollectResultRequest = new DepressCollectResultRequest();
        depressCollectResultRequest.setCollectId(collectId);
        depressCollectResultRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        depressCollectResultRequest.setDelFlag(DelFlagEnum.NO.getValue());
        paramReq.setEntity(depressCollectResultRequest);
        List<DepressCollectResultVo> results = depressCollectResultMapper.listPage(paramReq);
        return results;
    }

    @Override
    public Long save(DepressCollectRequest request) throws SaveException {
        depressCollectMapper.save(request);
        operaDepressDetail(request.getId(), request.getSelectIds());

        depressCollectResultMapper.updateDelFlagByCollectId(request.getId());
        List<DepressCollectResultRequest> resultRequests = request.getCollectResults();
        for (DepressCollectResultRequest result : resultRequests) {
            result.setStatusFlag(StatusFlagEnum.USE.getValue());
            result.setDelFlag(DelFlagEnum.NO.getValue());
            result.setCollectId(request.getId());
            depressCollectResultMapper.save(result);
        }
        return request.getId();
    }

    @Override
    public Long update(DepressCollectRequest request) throws UpdateException {
        depressCollectMapper.update(request);
        operaDepressDetail(request.getId(), request.getSelectIds());

        depressCollectResultMapper.updateDelFlagByCollectId(request.getId());
        List<DepressCollectResultRequest> resultRequests = request.getCollectResults();
        for (DepressCollectResultRequest result : resultRequests) {
            result.setStatusFlag(StatusFlagEnum.USE.getValue());
            result.setDelFlag(DelFlagEnum.NO.getValue());
            result.setCollectId(request.getId());
            depressCollectResultMapper.save(result);
        }
        return request.getId();

    }

    private void operaDepressDetail(Long collectId, List<Long> selectQuestionId) {
        // 删除数据
        int delCount = depressCollectDetailMapper.updateDelFlagByCollectId(collectId);
        if (ObjectUtil.isNotEmpty(selectQuestionId) && selectQuestionId.size() > 0) {
            depressCollectDetailMapper.batchSave(collectId, selectQuestionId);
        }
    }

    @Override
    public List<DepressCollectVo> listPage(ParamReq<DepressCollectRequest> t) throws SearchException {
        t.setStartIndex((t.getCurrent() - 1) * t.getSize());
        List<DepressCollectVo> res = depressCollectMapper.listPage(t);
        res.forEach(collectPo -> {
            collectPo.setStatusFlagName(StatusFlagEnum.getStatusValues(collectPo.getStatusFlag()));
            List<Long> questionIds = depressCollectDetailMapper.getQuestionIdsByCollectId(collectPo.getId());
            if (ObjectUtil.isEmpty(questionIds)) {
                questionIds = new ArrayList<>();
            }
            collectPo.setSelectIds(questionIds);

            List<DepressCollectResultVo> dcr = findResult(collectPo.getId());
            collectPo.setCollectResults(dcr);

        });
        return res;
    }

    @Override
    public Integer countPage(ParamReq<DepressCollectRequest> t) throws SearchException {
        return depressCollectMapper.countPage(t);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) throws DeleteException {
        return depressCollectMapper.updateDelFlagById(id, delFlag);
    }
}
